package com.example.chatroom.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.chatroom.service.ChatService;
import com.example.chatroom.websocket.WebSocketServer;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/chatroom")
public class TestChatRoom {
    @Autowired
    WebSocketServer webSocketServer;
    @Autowired
    ChatService chat;

    @GetMapping("/come")
    public String toChatroom() {
        try{
            webSocketServer.run();
        }finally {
            webSocketServer.close();
        }
        return "chatroom";
    }

    @PostMapping("/register")
    public String register(JSONObject param, ChannelHandlerContext ctx){
        chat.register(param,ctx);
        return "OK";
    }

}
